﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;

namespace MkvSplicer
{
    static class FilePaths
    {
        public static readonly string baseFolder = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
        public static readonly string toolsFolder = Path.Combine(baseFolder, "mkvsplicer-tools");
        public static readonly string mkvmerge = Path.Combine(toolsFolder, "mkvtoolnix", "mkvmerge.exe");
        public static readonly string mkvextract = Path.Combine(toolsFolder, "mkvtoolnix", "mkvextract.exe");
        //public static readonly string mkvinfo = Path.Combine(toolsFolder, "mkvtoolnix", "mkvinfo.exe");
        public static readonly string x264_8bit = Path.Combine(toolsFolder, "x264", "x264.exe");
        public static readonly string x264_10bit = Path.Combine(toolsFolder, "x264", "x264_10bit.exe");
        public static readonly string h264_parse = Path.Combine(toolsFolder, "h264_parse", "h264_parse.exe");
        public static readonly string ffms2 = Path.Combine(toolsFolder, "ffms2", "ffms2.dll");
        public const string ffms2_relative = "mkvsplicer-tools\\ffms2\\ffms2.dll";
        public static readonly string mediainfo = Path.Combine(toolsFolder, "mediainfo", "mediainfo.dll");
        public const string mediainfo_relative = "mkvsplicer-tools\\mediainfo\\mediainfo.dll";
        
        public static string getMissingToolsList()
        {
            List<string> missing = new List<string>(5);
            if (!File.Exists(mkvmerge))
                missing.Add("mkvmerge.exe");
            if (!File.Exists(mkvextract))
                missing.Add("mkvextract.exe");
            if (!File.Exists(x264_8bit))
                missing.Add("x264.exe");
            if (!File.Exists(x264_10bit))
                missing.Add("x264_10bit.exe");
            if (!File.Exists(ffms2))
                missing.Add("ffms2.dll");
            if (!File.Exists(mediainfo))
                missing.Add("mediainfo.dll");

            return String.Join(", ", missing);
        }
    }

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            /*if (Properties.Settings.Default.callUpgrade)
            {
                Properties.Settings.Default.Upgrade();
                Properties.Settings.Default.callUpgrade = false;
            }*/
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            string missing = FilePaths.getMissingToolsList();
            if (missing.Length > 0)
            {
                MessageBox.Show("The following required tools are missing: \r\n" +
                    missing + "\r\n" +
                    "Place the required tools as directed into the folder called " +
                    "'mkvsplicer-tools' to continue.", "Required tools are missing");
                return;
            }
            Application.Run(new MainForm());
        }
    }
}
